﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PhanCongGiangDay
{
    public class CGiangVien
    {
       //lay danh sach giang vien theo ma giang vien
        public static List<MGiangVien> getGVById(string idGV)
        {
           PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
           var gvs = from gv in db.GIANGVIENs
                     where gv.MAGV == idGV
                     select new MGiangVien
                     {
                         MAGV = gv.MAGV,
                         HOTEN =gv.HOTEN,
                         MABM = gv.MABM,
                         TENBM = gv.BOMON.TENBM
                     };
                         
           return gvs.ToList<MGiangVien>();

        }
        //lay danh sach tat ca giang vien cung voi mon ma giang vien do day
        public static List<MGiangVien> getAllGV()
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            var gvs = from gv in db.GIANGVIENs 
                      join bm in db.BOMONs      
                      on gv.MABM equals bm.MABM
                      select new MGiangVien
                      {
                          MAGV = gv.MAGV,
                          HOTEN = gv.HOTEN,
                          MABM = gv.MABM,
                          TENBM = bm.TENBM
                      };

            return gvs.ToList<MGiangVien>();
        }
        // them mot giang vien moi
        public static int addGV(string maGV, string tenGV, string maBM)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                GIANGVIEN gv = new GIANGVIEN
                {
                    MAGV = maGV,
                    HOTEN = tenGV,
                    MABM = maBM
                };
               db.GIANGVIENs.InsertOnSubmit(gv);
               db.SubmitChanges();
               return 1;
            }
            catch(Exception ex)
            {
                return -1;
            }
        }
        //cap nhat thong tin giang vien
        public static int updateGV(string maGV, string tenGV, string maBM)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                GIANGVIEN gv = (from gvObject in db.GIANGVIENs
                               where gvObject.MAGV == maGV
                               select gvObject).First();
                gv.HOTEN = tenGV;
                gv.MABM = maBM;                
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        //xoa di mot giang vien theo ma giang vien
        public static int deleteGV(string maGV)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            try
            {
                GIANGVIEN gv = db.GIANGVIENs.Where(g => g.MAGV == maGV).Single();
                db.GIANGVIENs.DeleteOnSubmit(gv);
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public static List<MGiangVien> findGV(string maHK, string namHoc, bool phanCong)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            
            List<MGiangVien> list = new List<MGiangVien>();
                var pcs = from pc in db.PHANCONGGIANGDAYs
                          where pc.MAHK == maHK
                          join gv in db.GIANGVIENs
                          on pc.MAGV equals gv.MAGV
                          select new MGiangVien
                          {
                              MAGV = gv.MAGV,
                              HOTEN = gv.HOTEN,
                              TENBM = gv.BOMON.TENBM
                          };
                List<MGiangVien> list1 = new List<MGiangVien>();
                list1 = pcs.ToList();
                if (!phanCong){
                     var gvs = from gv in db.GIANGVIENs                         
                          select new MGiangVien
                          {
                              MAGV = gv.MAGV,
                              HOTEN = gv.HOTEN,
                              TENBM = gv.BOMON.TENBM
                          };
                    List<MGiangVien> list2 = new List<MGiangVien>();
                    list2 = gvs.ToList();
                    for(int i=0;i<list2.Count;i++)
                    {
                        for(int j =0;j<list1.Count;j++)
                        {
                            if(list2[i].MAGV == list1[j].MAGV){
                                break;
                            }
                            if(j == list1.Count - 1){
                                list.Add(list2[i]);
                            }
                        }
                    }
                    return list;
                }
                else
                {
                    return list1;
                }  
        }

        public static object getGVThinhGiang(string maHK)
        {
            PhanCongGiangDayDataContext db = new PhanCongGiangDayDataContext();
            var gvs = from gv in db.GIANGVIENs
                      where gv.MABM == "" || gv.MABM == "TG"
                      join pc in db.PHANCONGGIANGDAYs
                      on gv.MAGV equals pc.MAGV where pc.MAHK == maHK
                      join hp in db.HOCPHANs
                      on pc.MAHP equals hp.MAHP
                      select new
                      {
                          MaGV = gv.MAGV,
                          TenGV = gv.HOTEN,
                          MaHP = hp.MAHP,
                          TenHP = hp.TENHP,
                          SoTTN = pc.SOTHUTUNHOM
                      };
            return gvs.ToList();
        }
    }
}
